//
//  ProfileVC.swift
//  Hyphenate-Demo-Swift
//
//  Created by cydida on 2018/12/3.
//  Copyright © 2018年 curtainups. All rights reserved.
//

import UIKit
import Hyphenate

class ProfileVC: UITableViewController {

    @IBOutlet weak var firstNameLabel: UILabel!
    @IBOutlet weak var lastNameLabel: UILabel!
    @IBOutlet weak var genderLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var countryLabel: UILabel!
    
    @IBOutlet weak var avatarImgView: UIImageView!
    @IBOutlet weak var userNameLabel: UILabel!
    
    @IBOutlet weak var avatarBorderView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)
        
        avatarImgView.layer.cornerRadius = 45;
        avatarImgView.layer.masksToBounds = true;
        avatarBorderView.layer.cornerRadius = 50
        avatarBorderView.layer.masksToBounds = true

        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = true
        
        getProfile()
    }
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }

    @IBAction func settingAction(_ sender: Any) {
        
        let storyboard2 = UIStoryboard.init(name: "Settings", bundle: nil)
        let settingsVC  = storyboard2.instantiateViewController(withIdentifier: "EMSettingsViewController") as? EMSettingsViewController
        navigationController?.pushViewController(settingsVC!, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 6:
            let storyboard2 = UIStoryboard.init(name: "ModifyProfileVC", bundle: nil)
            let settingsVC  = storyboard2.instantiateInitialViewController() as? ModifyProfileVC
            navigationController?.pushViewController(settingsVC!, animated: true)
        case 7:
            let storyboard2 = UIStoryboard.init(name: "feedback", bundle: nil)
            let settingsVC  = storyboard2.instantiateInitialViewController() as? FeedbackVC
            navigationController?.pushViewController(settingsVC!, animated: true)
        default:
            break
        }
    }
    
    //获取旧的Profile信息
    func getProfile() {
        
        //根据loginOwner合成urlstr
        var urlString : String = ""
        
        urlString = "profile?username=" + EMClient.shared().currentUsername
        
        let cuHTTP = CUHTTP.instance()
        
        weak var weakSelf = self
        
        cuHTTP.makeGetData(url: urlString, success: {response in
            print("Response String: \(String(describing: response?.result.value))")
            print(response?.response?.statusCode ?? "??")      //成功时，返回200
            if let value = response?.result.value {
                let data = value.data(using: String.Encoding.utf8)! as NSData
                let json = try? JSONSerialization.jsonObject(with: data as Data,
                                                             options:.allowFragments) as! [String: Any]
                print(json ?? "??")
                let allEvents = json!["user"] as Any
                print(allEvents)
                
                let loopE = allEvents as! [String: Any]
                if let tmpStr0 = loopE["username"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.userNameLabel.text = tmpStr1
                    }
                }
                if let tmpStr0 = loopE["city"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.cityLabel.text = tmpStr1
                    }
                }
                if let tmpStr0 = loopE["firstName"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.firstNameLabel.text = tmpStr1
                    }
                }
//                if let tmpStr0 = loopE["middleName"] {
//                    if let tmpStr1 = (tmpStr0 as? String) {
//                        self.middleName = tmpStr1
//                        self.middleNameView.text = self.middleName
//                    }
//                }
                if let tmpStr0 = loopE["lastName"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.lastNameLabel.text = tmpStr1
                    }
                }
                if let tmpStr0 = loopE["country"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
//                        let tmpStr2 = EnStatic.getWord(keyWord: tmpStr1)
//                        self.country = tmpStr2
//                        self.countryButton.setTitle(self.country, for: .normal)
//                        self.countryView.text = self.country
                        let tmpStr2 = EnStatic.getWord(keyWord: tmpStr1)
                        weakSelf?.countryLabel.text = tmpStr2
                    }
                }
//                if let tmpStr0 = loopE["deviceType"] {
//                    if let tmpStr1 = (tmpStr0 as? String) {
//                        self.deviceType = tmpStr1
//                    }
//                }
//                if let tmpStr0 = loopE["dob"] {
//                    if let tmpStr1 = (tmpStr0 as? String) {
//                        weakSelf?.birthdayLabel.text = tmpStr1
//                    }
//                }
                if let tmpStr0 = loopE["gender"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        let tmpStr2 = EnStatic.getWord(keyWord: tmpStr1)
                        weakSelf?.genderLabel.text = tmpStr2
                    }
                }
//                if let tmpStr0 = loopE["password"] {
//                    if let tmpStr1 = (tmpStr0 as? String) {
//                        self.password = tmpStr1
//                    }
//                }
                if let tmpStr0 = loopE["phone"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.phoneLabel.text = tmpStr1
                    }
                }
               
                
                if let tmpStr0 = loopE["countryCode"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
                        weakSelf?.phoneLabel.text = "(+" + tmpStr1 + ")" + (weakSelf?.phoneLabel.text)!
                    }
                }
                //读取头像文件全路径
                if let tmpStr0 = loopE["imageName"] {
                    if let tmpStr1 = (tmpStr0 as? String) {
//                        self.imagePath = tmpStr1
                        weakSelf?.avatarImgView.imageWithUsername(username: EMClient.shared().currentUsername, placeholderImage: nil)
                    }
                    
                }
//                self.updateSaveButtonState()
                
            }
        },
                           failure:{ response in
                            print(response ?? " ")
                           weakSelf?.show(EnStatic.getWord(keyWord: "get data failed"))
        }
        )
        
    }
}
